package com.devin.controller;

import com.devin.pojo.Permission;
import com.devin.pojo.Role;
import com.devin.service.RoleService;
import com.github.pagehelper.PageInfo;
import com.sun.javafx.collections.ImmutableObservableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/getRoles")
    public ModelAndView getRoles(@RequestParam(value = "page",defaultValue = "1")Integer page
            ,@RequestParam(value = "size",defaultValue = "5")Integer size){
        List<Role> roles = roleService.getRoles(page, size);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("rolePageInfo",rolePageInfo);
        modelAndView.setViewName("pages/role-pages-list");
        return modelAndView;
    }

    @RequestMapping("/getRoles2UserAdded")
    public ModelAndView getRoles2UserAdded(){
        List<Role> roles = roleService.getRoles2UserAdded();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles",roles);
        modelAndView.setViewName("pages/user-add");
        return modelAndView;
    }
    @RequestMapping(value = "/addRoleWithPermission",method = RequestMethod.GET)
    public ModelAndView addRoleWithPermission(){
        List<Permission> permissions=roleService.addRoleWithPermission();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permissions",permissions);
        modelAndView.setViewName("pages/role-add");
        return modelAndView;
    }

    @RequestMapping("/addRole")
    public String addRole(Role role,Integer[] permissionIds){
        roleService.addRole(role,permissionIds);
        return "redirect:/roles/getRoles";
    }

    @RequestMapping(value = "/getRoleById",method = RequestMethod.GET)
    public ModelAndView getRoleById(Integer roleId){
        Role role =roleService.getRoleById(roleId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("pages/role-detail");
        return modelAndView;
    }

    @RequestMapping(value = "/delPermissionByRoleId",method = RequestMethod.GET)
   public String delPermissionByRoleId(Integer roleId,Integer permissionId){
        roleService.delPermissionByRoleId(roleId,permissionId);
        return "redirect:/roles/getRoleById?roleId="+roleId;
   }

   @RequestMapping("/getPermissions2Json")
   @ResponseBody
   public List<Permission> getPermissions2Json(Integer roleId){
       List<Permission> permissions2Json = roleService.getPermissions2Json(roleId);
       return permissions2Json;
   }
   @RequestMapping(value = "/addPermissions",method = RequestMethod.POST)
    public String addPermissions(Integer roleId,Integer[] permissionIds){
        roleService.addPermissions(roleId,permissionIds);
        return "redirect:/roles/getRoleById?roleId="+roleId;
   }
   @RequestMapping("/delRole")
    public String delRole(Integer roleId){
        roleService.delRole(roleId);
        return  "redirect:/roles/getRoles";
   }

   @RequestMapping(value = "/delRolesChecked",method = RequestMethod.POST)
    public String delRolesChecked(Integer[] ids){
        roleService.delRolesChecked(ids);
        return "redirect:/roles/getRoles";
   }
}
